<html>
	<head>
		<title>JavaScript Test Page</title>
		<script type="text/javascript" src="http://www.google.com/jsapi"></script>
		<script type="text/javascript" language="JavaScript">
			google.load("gdata", "2", {packages: ["finance"]});
		</script>
	</head>
	<body>
		<img src="http://localhost/~mferrare/gfportfoliogadget/pic.jpg" width=10 height=10 alt="pic">
		<form name="inputform">
		<input type=button name="login" value="Login" onclick="logMeIn()">
		<input type=button name="logout" value="Logout" onclick="logMeOut()">
		<input type=button name="cleardebug" value="Clear Debug Window" onclick="clearDebug()">
		<input type=button name="getmyfeed" value="Get My Feed" onclick="getMyFeed()">
		<input type=text name="ready" value="0">
		<input type=text name="loggedin" value="">
		</form>
		<div id="debugdiv">Debug Output</div>
		<script type="text/javascript" language="JavaScript">

			google.setOnLoadCallback(setReady);
			var portfolioFeedUri = 'http://finance.google.com/finance/feeds/default/portfolios';
			var ready = 0

			function setReady() {
				document.forms["inputform"]["ready"].value = 1;
				document.forms["inputform"]["loggedin"].value = "not sure: " + google.accounts.user.getInfo();

			}

			function getReady() {
				return document.forms["inputform"]["ready"].value;
			}

			function logMeIn() {
				scope = "http://www.google.com/finance/feeds/";
				var token = google.accounts.user.login(scope);
				document.forms["inputform"]["loggedin"].value = "logged in: " + google.accounts.user.login.getInfo();
			}

			function logMeOut() {
				google.accounts.user.logout();
				document.forms["inputform"]["loggedin"].value = "logged out: " + google.accounts.user.login.getInfo();
			}

			function setupMyService() {
				var myService = new google.gdata.finance.FinanceService('GoogleInc-financejsguide-1.0');
				return myService;
			}

			function getMyFeed() {
				if ( getReady() == 1 ) {
					debug('Retrieving a list of the user portfolios...');
					myService = setupMyService();
					myService.getPortfolioFeed(portfolioFeedUri,
					                                portfolioFeedCallback,
					                                handleErrorCallback);
				} else {
					debug("not ready: " + getReady());
				}
			}

			function debug(msg) {
			  b = document.createElement("p");
			  b.appendChild(document.createTextNode(msg))
			  debugdiv = document.getElementById("debugdiv");
			  debugdiv.appendChild(b);
			}

			function clearDebug() {
				document.getElementById("debugdiv").innerHTML = "";
			}

			// This callback will run when the portfolio query is complete
			function portfolioFeedCallback(result) {
			  debug("about to enter portfolioFeedCallback(" + result + ")");
			  // An array with all of the user's portfolios
			  var entries = result.feed.entry;
			  for (var i = 0; i < entries.length; i++) {
			    var portfolioEntry = entries[i];
			    var portfolioTitle = portfolioEntry.getTitle().getText();
			    debug('Portfolio ' + (i+1) + ': ' + portfolioTitle);
			  }
			};

			// FinanceService methods may be supplied with an alternate callback for errors
			function handleErrorCallback(e) {
			  debug("about to enter handleErrorCallback(" + e + ")");
			  debug("portfolioFeedUri: " + portfolioFeedUri);
			  debug("myService: " + myService);
			  debug(e.cause ? e.cause.statusText : e.message);
			  debug(e.toString());
			};


		</script>